<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 7/18/2016
 * Des: 系统管理员
 */
defined('INHkMvc') or exit('Access Invalid!');

class App_good_2_vipModel extends AppModel
{

    public function getSpreadClickUrlByUrl($url)
    {
        if (isset($url) && $url != NULL) {
            $data = getSpreadClickUrl($url);
            if ($data['total_results'] > 0) {
                if ($data['results']['tbk_spread'][0]['err_msg'] == 'OK') {
                    return $data['results']['tbk_spread'][0]['content'];
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function checkUrl($good_id, $member_id = 1)
    {
        $res = $this->field("url,short_url")->find(['member_id' => $member_id, 'good_id' => $good_id]);
        if ($res) {
            if ($res['url'] == "" || $res['short_url'] == "") {
                return "empty";
            } else {
                return false;
            }
        } else {
            return "new";
        }
    }

    public function updateDefaultUrl($url, $good_id, $type)
    {

        $data = [];
        $data['url'] = $url['clickUrl'];
        $data['short_url'] = $url['shortLinkUrl'];

        if ($type == "new") {
            $data['member_id'] = 1;
            $data['good_id'] = $good_id;
            $data['add_time'] = time();
            $res = $this->insert($data);
        } elseif ($type == "empty") {
            $res = $this->update($data, ['member_id' => 1, 'good_id' => $good_id]);
        } else {
            return false;
        }
        return $res;
    }
    public function getCollectUrlByCondition($all_url,$good_id,$member_id,$site_name,$site_id){
        //if(preg_match('//i', $all_url)){
            if (isset($all_url['data']) && !empty($all_url['data'])) {
                    $good_2_vip_id=$this->field('id')->find(['good_id' => $good_id, 'member_id' => $member_id,'site_name' => $site_name,'site_id' => $site_id])['id'];
                    $data=[];
                    if(isset($all_url['data']['couponShortLinkUrl']) && !empty($all_url['data']['couponShortLinkUrl'])){
                        if (!empty($good_2_vip_id)) {
                            $res=$this->update(['coupon_url' => $all_url['data']['couponShortLinkUrl'],'update_time' => time()], ['id'=>$good_2_vip_id]);
                        } else {
                            $res=$this->insert(['coupon_url'=>$all_url['data']['couponShortLinkUrl'],'short_url' => $all_url['data']['shortLinkUrl'],'url' => $all_url['data']['clickUrl'],'member_id' => $member_id, 'good_id' => $good_id, 'add_time' => time(),'update_time' => time(),'site_name' => $site_name,'site_id' => $site_id]);
                            $good_2_vip_id=$res;
                        }
                        $data['click_url']=$all_url['data']['couponShortLinkUrl'];
                    }else if (isset($all_url['data']['shortLinkUrl']) && !empty($all_url['data']['shortLinkUrl'])) {
                        if (!empty($good_2_vip_id)) {
                            $res=$this->update(['short_url' => $all_url['data']['shortLinkUrl'], 'update_time' => time()], ['id'=>$good_2_vip_id]);
                        } else {
                            $res=$this->insert(['short_url' => $all_url['data']['shortLinkUrl'],'url' => $all_url['data']['clickUrl'],'member_id' => $member_id, 'good_id' => $good_id, 'add_time' => time(),'update_time' => time(),'site_name' => $site_name,'site_id' => $site_id]);
                            $good_2_vip_id=$res;
                        }
                        $data['click_url']=$all_url['data']['shortLinkUrl'];
                    } else {
                        if (isset($all_url['data']['clickUrl']) && !empty($all_url['data']['clickUrl'])) {
                            $short_url=getShortClickUrlByUrl($all_url['data']['clickUrl'],$site_name);
                            if($short_url!=false){
                                if (!empty($good_2_vip_id)) {
                                    $res=$this->update(['url' => $all_url['data']['clickUrl'],'short_url' =>$short_url,'update_time' => time()],['id'=>$good_2_vip_id]);
                                } else {
                                    $res=$this->insert(['url' => $all_url['data']['clickUrl'],'short_url' =>$short_url, 'member_id' => $member_id, 'good_id' => $good_id, 'add_time' => time(),'update_time' => time(),'site_name' => $site_name,'site_id' => $site_id]);
                                    $good_2_vip_id=$res;
                                }
                                $data['click_url']=$short_url;
                            }else{
                                if (!empty($good_2_vip_id)) {
                                    $res=$this->update(['url' => $all_url['data']['clickUrl'],'update_time' => time()],['id'=>$good_2_vip_id]);
                                } else {
                                    $res=$this->insert(['url' => $all_url['data']['clickUrl'],'member_id' => $member_id, 'good_id' => $good_id, 'add_time' => time(),'update_time' => time(),'site_name' => $site_name,'site_id' => $site_id]);
                                    $good_2_vip_id=$res;
                                }
                                $data['click_url']=$all_url['data']['clickUrl'];
                            }    
                        } else {
                            return false;
                        }
                    }
                    if($res===false){
                       return false; 
                    }else{
                        $data['site_name']=$site_name;
                        $data['good_2_vip_id']=$good_2_vip_id;
                       return $data; 
                    }
            } else {
               return false;     
            }
        // }else{
        //     return 'spider';
        // }   
    }
    public function getCollectUrlAngin($num_iid,$ad_zone_id,$good_id,$member_id,$site_name,$site_id,$siteId){
        $i=0;
        while ($i<3) {
            if($i!=0){
              sleep(3);
            }
            $data = getClick($num_iid,$ad_zone_id,$site_name,$siteId);
            if ($this->getCollectUrlByCondition($data,$good_id,$member_id,$site_name,$site_id)) {
                break;
            }else{
                $i++;
            }
            if($i==3){
               return false; 
            }
        }
        return $this->getCollectUrlByCondition($data,$good_id,$member_id,$site_name,$site_id);
    }
    public function getCollectUrlByAllSite($num_iid,$good_id,$member_id,$site){
        $i=0;
        while ($i<count($site)) {
            $ad_zone_id=distributionAdZoneId($member_id,$site[$i]['app']);
            if($ad_zone_id==false){
                return false;
            }
            $data = $this->getCollectUrlAngin($num_iid,$ad_zone_id,$good_id,$member_id,$site[$i]['app'],$site[$i]['id'],$site[$i]['site_id']);
            if (!empty($data) && $data!='spider') {
                break;
            }else{
                $i++;
            } 
            if($i>=count($site)){
                return false;
            }  
        }
        return $data;  
    }
}
